package com.android.lovingpet.mine.myInformation;

import android.animation.ObjectAnimator;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.base.TakePhotoActivity;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.util.ImageUtil;
import com.android.lovingpet.util.Utility;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jph.takephoto.model.TResult;

import java.io.File;

import de.hdodenhof.circleimageview.CircleImageView;

public class MyinformationActivity extends TakePhotoActivity<IMyinformationContract.View,MyinformationPresenter> implements View.OnClickListener,IMyinformationContract.View, PopupMenu.OnMenuItemClickListener {

    private CircleImageView ivImage;
    private EditText et_name;
    private TextView tv_level;
    private TextView tv_contribution;
    private AppCompatSpinner sex;
    private EditText et_synopsis;
    private EditText et_city;
    private File head;
    private Account account;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myinformation);
        ((TextView) findViewById(R.id.tv_title)).setText("个人资料");
        ivImage =findViewById(R.id.iv_head);
        et_name=findViewById(R.id.et_name);
        tv_level=findViewById(R.id.tv_level);
        tv_contribution=findViewById(R.id.tv_contribution);
        sex=findViewById(R.id.spinner_gender);
        et_synopsis=findViewById(R.id.et_synopsis);
        et_city=findViewById(R.id.et_city);
        account = Utility.getAccount();

        ivImage.setOnClickListener(this);
        findViewById(R.id.fab_yes).setOnClickListener(this);
        displayInfo();
    }
    @Override
    public MyinformationPresenter createPresenter() {
        return new MyinformationPresenter();
    }

    @Override
    public IMyinformationContract.View createView() {
        return this;
    }

    @Override
    public void next() {
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_head:
                PopupMenu popup = new PopupMenu(this, ivImage);
                //Inflating the Popup using xml file
                popup.getMenuInflater()
                        .inflate(R.menu.popup_menu_selectphoto, popup.getMenu());
                popup.setOnMenuItemClickListener(this);
                popup.show();
                break;
            case R.id.fab_yes:
                String name=et_name.getText().toString();
                int gender=sex.getSelectedItemPosition();
                String synopsis=et_synopsis.getText().toString();
                String city=et_city.getText().toString();
                getPresenter().changeInfo(head, account.getRealId(), Utility.getUserType(), name, gender, synopsis, city);
                break;
        }
    }

    @Override
    public void takeSuccess(TResult result) {
        String iconPath = result.getImage().getOriginalPath();
        head = new File(iconPath);
        Glide.with(this)
                .load(head)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(ivImage);
    }

    @Override
    public void takeFail(TResult result, String msg) {
        showToast("Error:" + msg);
    }

    @Override
    public void takeCancel() {

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.take_photo:
                imageUri = getImageCropUri();
                takePhoto.onPickFromCaptureWithCrop(imageUri, cropOptions);
                break;
            case R.id.select_photo:
                imageUri = getImageCropUri();
                takePhoto.onPickFromGalleryWithCrop(imageUri, cropOptions);
                break;
            default:
                break;
        }
        return false;
    }

    private void displayInfo() {
        final ObjectAnimator anim = ObjectAnimator.ofInt(ivImage, "ImageLevel", 0, 10000);
        anim.setDuration(800);
        anim.setRepeatCount(ObjectAnimator.INFINITE);
        anim.start();
        Glide.with(this).load(account.getHead()).placeholder(R.drawable.defaultface)
                .error(R.drawable.defaultface).into(ivImage);
        et_name.setText(account.getName());
        tv_level.setText(String.valueOf(account.getGrade()));
        tv_contribution.setText(String.valueOf(account.getLoveValue()));
        sex.setSelection(account.getGender());
        et_synopsis.setText(account.getSynopsis());
        et_city.setText(account.getCity());
    }
}
